package com.it.schoolbookshop_back.service.impl;

import com.it.schoolbookshop_back.entities.po.User;
import com.it.schoolbookshop_back.mapper.UserMapper;
import com.it.schoolbookshop_back.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.findByUsernameAndPassword(username, password);
    }

    @Override
    public Integer register(User user) {
        // 检查用户名是否已存在
        if (isUsernameExists(user.getUsername())) {
            return null;
        }
        
        // 插入用户
        int result = userMapper.insert(user);
        return result > 0 ? user.getId() : null;
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.findById(id);
        if (user != null) {
            System.out.println("==== 从数据库获取的用户信息 ====");
            System.out.println("ID: " + user.getId() + ", 类型: " + (user.getId() != null ? user.getId().getClass().getName() : "null"));
            System.out.println("用户名: " + user.getUsername() + ", 类型: " + (user.getUsername() != null ? user.getUsername().getClass().getName() : "null"));
            System.out.println("角色: " + user.getRole() + ", 类型: " + (user.getRole() != null ? user.getRole().getClass().getName() : "null"));
            System.out.println("学号: " + user.getStudent_id() + ", 类型: " + (user.getStudent_id() != null ? user.getStudent_id().getClass().getName() : "null"));
            System.out.println("实名: " + user.getReal_name() + ", 类型: " + (user.getReal_name() != null ? user.getReal_name().getClass().getName() : "null"));
            System.out.println("院系: " + user.getDepartment() + ", 类型: " + (user.getDepartment() != null ? user.getDepartment().getClass().getName() : "null"));
            System.out.println("邮箱: " + user.getEmail() + ", 类型: " + (user.getEmail() != null ? user.getEmail().getClass().getName() : "null"));
            System.out.println("电话: " + user.getPhone() + ", 类型: " + (user.getPhone() != null ? user.getPhone().getClass().getName() : "null"));
            System.out.println("微信: " + user.getWechat() + ", 类型: " + (user.getWechat() != null ? user.getWechat().getClass().getName() : "null"));
            System.out.println("简介: " + user.getBio() + ", 类型: " + (user.getBio() != null ? user.getBio().getClass().getName() : "null"));
            System.out.println("头像类型: " + user.getAvatar_type() + ", 类型: " + (user.getAvatar_type() != null ? user.getAvatar_type().getClass().getName() : "null"));
            
            // 添加数据库查询调试
            try {
                User freshUser = userMapper.findById(id);
                // 打印原始结果集
                System.out.println("===== 重新查询用户信息 =====");
                System.out.println("学号字段值: " + freshUser.getStudent_id());
                System.out.println("实名字段值: " + freshUser.getReal_name());
                System.out.println("===========================");
            } catch (Exception e) {
                System.out.println("调试查询异常: " + e.getMessage());
                e.printStackTrace();
            }
            
            System.out.println("=============================");
        }
        return user;
    }

    @Override
    public boolean isUsernameExists(String username) {
        User user = userMapper.findByUsername(username);
        return user != null;
    }
    
    @Override
    public boolean updateUser(User user) {
        // 不允许修改用户名和密码
        if (user.getId() == null) {
            return false;
        }
        
        // 执行更新
        int result = userMapper.update(user);
        return result > 0;
    }
} 